var vm = new Vue({
	el : '#rrapp',
	data : {
		q : {
			memberId : "",
			mobile : "",
			startDate : "",
			endDate : "",
		},
		teams : 0,
		sumOne : 0,
		sumTwo : 0,
		sumThree : 0,
		sumFour : 0,
		sumFive : 0,
		sumSix : 0,
		prifit : 0
	},
	methods : {
		query : function() {
			if (isBlank(this.q.memberId) && isBlank(this.q.mobile)) {
				alert("请填写会员ID或手机号");
				return;
			}

			$.getJSON(baseURL + "biz/team/select/", vm.q , function(result) {
					var root = [];
					if (result.accounting) {
						root.push(result.accounting);
						vm.teams = Big(result.accounting.countOne).plus(result.accounting.countTwo).plus(result.accounting.countThree).toString();
						vm.sumOne = Big(result.accounting.sumOne).plus(result.countDto.sumOne).toString();
						vm.sumTwo = Big(result.accounting.sumTwo).plus(result.countDto.sumTwo).toString();
						vm.sumThree = Big(result.accounting.sumThree).plus(result.accounting.sumSeven).plus(result.countDto.sumThree).plus(result.countDto.sumSeven).abs().toString();
						vm.sumFour = Big(result.accounting.sumFour).plus(result.countDto.sumFour).toString();
						vm.sumFive = Big(result.accounting.sumFive).plus(result.countDto.sumFive).toString();
						vm.sumSix = Big(result.accounting.sumSix).plus(result.countDto.sumSix).toString();
						vm.prifit = Big(vm.sumFour).plus(vm.sumSix).plus(vm.sumFive).minus(vm.sumThree).toString();
					} else {
						parent.layer.msg(result.msg);
					}
					$.fn.zTree.init($("#dataTree"), setting, root);
			});
		}
	}
});

var setting = {
	data : {
		key : {
			name : "id"
		}
	},
	view : {
		addDiyDom : function(treeId, treeNode) {
			var node = $("#" + treeNode.tId + "_a");
			if(treeNode.mobile) {
				var html = "<span class='mg'><b>手机号:</b>" + treeNode.mobile + "</span>" +
						   "<span class='mg'><b>上级会员:</b>" + (treeNode.parentId ? "<a onclick='vm.q.memberId="+treeNode.parentId+";vm.query();'>" + treeNode.parentId : "无") + "</a></span>" +
						   "<span class='mg'><b>直推:</b>" + treeNode.countOne + "人</span>" +
						   "<span class='mg'><b>二级:</b>" + treeNode.countTwo + "人</span>" +
						   "<span class='mg'><b>三级:</b>" + treeNode.countThree + "人</span>" +
						   "<span class='mg'><b>充值:</b>￥" + treeNode.sumOne + "</span>" +
						   "<span class='mg'><b>提现:</b>￥" + treeNode.sumTwo + "</span>" +
						   "<span class='mg'><b>推广提成:</b>￥" + treeNode.sumSix + "</span>" +
						   "<span class='mg'><b>发包:</b>￥" + Big(treeNode.sumThree).plus(treeNode.sumSeven).abs() + "</span>" +
						   "<span class='mg'><b>抢包:</b>￥" + treeNode.sumFour + "</span>" + 
						   "<span class='mg'><b>雷点收益:</b>￥" + treeNode.sumFive + "</span>" +
						   "<span class='mg'><b>净收益:</b>￥" + Big(treeNode.sumFour).plus(treeNode.sumSix).plus(treeNode.sumFive).plus(treeNode.sumSeven).plus(treeNode.sumThree) + "</span>";
				node.after(html);
				var tree = $.fn.zTree.getZTreeObj(treeId);
				var childs = [];
				if(treeNode.countOne > 0) {
					childs.push({id: "直接推荐", isParent: true, userLevel: 1, mId: treeNode.id});
					if(treeNode.countTwo > 0) {
						childs.push({id:"二级推荐", isParent: true, userLevel: 2, mId: treeNode.id});
						if(treeNode.countThree > 0) {
							childs.push({id:"三级推荐", isParent: true, userLevel: 3, mId: treeNode.id});
						}
					}
					tree.addNodes(treeNode, -1, childs, true);
				}
			}
		}
	},
	callback : {
		onExpand: function(event, treeId, treeNode) {
			if(treeNode.mId) {
				if(!treeNode.isAjaxing) {
					$.getJSON(baseURL + "biz/team/subList", {memberId: treeNode.mId, level: treeNode.userLevel, startDate: vm.q.startDate, endDate: vm.q.endDate }, function(result) {
						treeNode.isAjaxing = true;
						var tree = $.fn.zTree.getZTreeObj(treeId);
						if(result.page.list.length > 0) {
							$.each(result.page.list, function(i, item) {
								tree.addNodes(treeNode, -1, item, true);
							});
							
							if(result.page.totalPage != result.page.currPage) {
								tree.addNodes(treeNode, -1, {id:"点击加载更多", currPage: result.page.currPage, parentId: treeNode.mId, userLevel: treeNode.userLevel}, true);
							}
						}
					});
				}
			}
		},
		onClick: function(event, treeId, treeNode) {
			if(treeNode.currPage) {
				$.getJSON(baseURL + "biz/team/subList", {memberId: treeNode.parentId, level: treeNode.userLevel, page: treeNode.currPage + 1 }, function(result) {
					var tree = $.fn.zTree.getZTreeObj(treeId);
					if(result.page.list.length > 0) {
						$.each(result.page.list, function(i, item) {
							tree.addNodes(treeNode.getParentNode(), -1, item, true);
						});
						if(result.page.totalPage != result.page.currPage) {
							tree.addNodes(treeNode.getParentNode(), -1, {id:"点击加载更多", currPage: result.page.currPage, parentId: treeNode.parentId, userLevel: treeNode.userLevel}, true);
						}
						//删除上次的[点击加载更多]按钮
						tree.removeNode(treeNode);
					}
				});
			}
		}
	}
};
